package model.solver;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import model.NoSuchDirectoryException;
import controller.solver.MiniSat;
import controller.solver.RSat;
import controller.solver.Solver;

/**
 * Manage the creation of preprocessor.
 * 
 * @author GRONOWSKI Stephane svan001@aol.com
 * 
 *         igsat 2010
 * 
 */
public class SolverFactory {

	private File			directory;
	private Set<ESolver>	available	= new HashSet<ESolver>();


	/**
	 * Create a solver factory.
	 * 
	 * @param directory
	 *            the directory where are the executable file.
	 */
	public SolverFactory(File directory) {
		this.directory = directory;
		for (ESolver p : ESolver.values())
			available.add(p);
	}

	/**
	 * @return the available solver.
	 */
	public Set<ESolver> getAvailable() {
		return available;
	}

	/**
	 * Create a solver of the specified type.
	 * 
	 * @param solver
	 *            solver needed.
	 * @return the solver, null if it can't be created.
	 * @throws NoSuchDirectoryException
	 *             if the directory where the executable file should be doesn't exist
	 */
	public Solver createSolver(ESolver solver) throws NoSuchDirectoryException {
		try
		{
			switch (solver) {
			case MINISAT:
				return new MiniSat(directory);
			case RSAT:
				return new RSat(directory);
			default:
				return null;
			}
		} catch (IOException e)
		{
			throw new NoSuchDirectoryException();
		}
	}

	/**
	 * Create a {@link MiniSat} solver.
	 * 
	 * @return the solver.
	 * @throws NoSuchDirectoryException
	 *             if the directory where the executable file should be doesn't exist
	 */
	public MiniSat createMiniSatSolver() throws NoSuchDirectoryException {
		try
		{
			return new MiniSat(directory);
		} catch (IOException e)
		{
			throw new NoSuchDirectoryException();
		}
	}

	/**
	 * Create a {@link RSat} solver.
	 * 
	 * @return the solver
	 * @throws NoSuchDirectoryException
	 *             if the directory where the executable file should be doesn't exist
	 */
	public RSat createRSatSolver() throws NoSuchDirectoryException {
		try
		{
			return new RSat(directory);
		} catch (IOException e)
		{
			throw new NoSuchDirectoryException();
		}
	}

	/**
	 * 
	 * @return the default directory where the executable file a re suppose to be.
	 */
	public File getDirectory() {
		return directory;
	}

	/**
	 * Set the default directory where the executable file a re suppose to be.
	 * 
	 * @param directory
	 *            the directory.
	 */
	public void setDirectory(File directory) {
		this.directory = directory;
	}

}
